<?php
class App_Admin_Service_Install
{
	/**
	 * @var array
	 */
	protected $_moduleConfigs;
	
	/**
	 * @var Main_Db_Adapter_Abstract
	 */
	protected $_db;
	
	/**
	 * @return void
	 */
	public function __construct()
	{
		$modules = Main_Module::getModules();
		unset($modules['default']); 
		
		$this->_db = Main_Db::get();
		
		foreach ($modules as $module) {
			$configName = 'App_' . ucfirst($module) . '_Service_DbStructure';
			$this->_moduleConfigs[] = new $configName($this->_db);
		}
	}
	
	/**
	 * @return void
	 */
    public function run() 
    {
    	$config = Zend_Registry::getInstance()->config;
    	$dbProfile = Zend_Registry::getInstance()->dbProfile;
    	
    	$adapter = $config['database'][$dbProfile]['adapter'];
    	if (array_key_exists('table_type', $driver_options = $config['database'][$dbProfile]['params']['driver_options'])) { 
        	$tableType = $driver_options['table_type'];
    	} else {
    		$tableType = null;
    	}	

		$sql = '';
		foreach ($this->_moduleConfigs as $moduleConfig) {		
			$sql .= $moduleConfig->getInstallSql($adapter, $driverType);
		}	
		$sqlArr = explode(';', $sql);
		
		foreach ($sqlArr as $sql) {
			$sql = trim($sql);
			if (!empty($sql)) {
				$this->_db->query($sql);
			}	
		}
		
		foreach ($this->_moduleConfigs as $moduleConfig) {
			$moduleConfig->setDefaultData();
		}
    }
    
}


